package com.liu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.bean.*;
import com.liu.service.PdjdtkService;
import com.liu.service.XztkService;
import com.liu.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class QuestionsController {
    @Autowired
    PdjdtkService pdjdtkService;
    @Autowired
    XztkService xztkService;
    @Autowired
    Utils utils;

    @RequestMapping("/test")
    @ResponseBody
    public void test(){
        Integer a=utils.randomPage(10,5);

    }
    /**
     * 判断题
     * @return
     */
    @RequestMapping("/pdjdtk")
    @ResponseBody
    public Map<String,Object> pdjdtk(Parameters parameters){
        System.out.println(parameters);
        Map<String,Object> map=new HashMap<>();
        PdjdtkExample example=new PdjdtkExample();
        PdjdtkExample.Criteria criteria=example.createCriteria();
        criteria.andBklxIdEqualTo(parameters.getBklx_id());
        criteria.andBkjbIdEqualTo(parameters.getBkjb_id());
        criteria.andKslxIdEqualTo(parameters.getKslx_id());
        criteria.andKstxIdEqualTo(parameters.getKstx_id());

            int length=pdjdtkService.count(example);
                if (length==0) {
                    map.put("judge",new ArrayList<>());
                    return map;
                }
            Integer page=utils.randomPage(length,parameters.getSize());
            System.out.println("判断题库长度:"+length+",page:"+page);
            PageHelper.startPage(page,parameters.getSize());
            List<PdjdtkWithBLOBs> pdjdtks=pdjdtkService.selectByExample(example);
            PageInfo<PdjdtkWithBLOBs> pageInfo=new PageInfo<>(pdjdtks);
            map.put("judge",pageInfo.getList());
            return map;
    }
    /**
     *选择题
     */
    @RequestMapping("xztk")
    @ResponseBody
    public Map<String,Object> xztk(Parameters parameters){
        System.out.println(parameters);
        Map<String,Object> map=new HashMap<>();
        XztkExample example=new XztkExample();
        XztkExample.Criteria criteria=example.createCriteria();
        criteria.andBklxIdEqualTo(parameters.getBklx_id());
        criteria.andBkjbIdEqualTo(parameters.getBkjb_id());
        criteria.andKslxIdEqualTo(parameters.getKslx_id());
        criteria.andKstxIdEqualTo(parameters.getKstx_id());

            int length=xztkService.count(example);
            if (length==0) {
                map.put("select",new ArrayList<>());
                return map;
            }
            Integer page=utils.randomPage(length,parameters.getSize());
            System.out.println("选择题库长度:"+length+",page:"+page);
            PageHelper.startPage(page,parameters.getSize());
            List<XztkWithBLOBs> xztkWithBLOBs=xztkService.selectByExample(example);
            PageInfo<XztkWithBLOBs> pageInfo=new PageInfo<>(xztkWithBLOBs);
            map.put("select",pageInfo.getList());
            return map;

    }
}
